package com.fhx.core.utils

import android.text.TextUtils
import com.nulabinc.zxcvbn.Zxcvbn
import java.util.regex.Matcher
import java.util.regex.Pattern


object StringUtil {
    /**
     * 是否是中国内的手机号
     */
    fun isChinaMobile(phone: String): Boolean {
//        val regex =
//            "^((13[0-9])|(14[1]|[4-9])|(15([0-3]|[5-9]))|(16([2]|[5-7]))|(17([0-3]|[5-8]))|(18[0-9])|(19[1|8|9]))\\d{8}$"
//        return if (phone.length != 11) {
//            false
//        } else {
//            val p = Pattern.compile(regex)
//            val m = p.matcher(phone)
//            m.matches()
//        }
        val p = Pattern.compile("^1\\d{10}$")
        val m = p.matcher(phone)
        return m.matches()
    }

    /**
     * 校验是否有邮箱
     */
    fun isEmail(email: String?): Boolean {
        val REGEX_EMAIL =
            "^[0-9A-Za-z][\\.-_0-9A-Za-z]*@[0-9A-Za-z]+(\\.[0-9A-Za-z]+)+$"
        return Pattern.matches(REGEX_EMAIL, email)
    }

    /**
     * 多个空白字符替换成一个空格
     */
    fun multiBlankTo1Space(str: String?): String? {
        return str?.replace(Regex("\\s+"), " ")
    }

    /**
     *  # score from 0-4 (useful for implementing a strength bar)
        # 0 Weak        （guesses < ^ 3 10）
        # 1 Fair        （guesses <^ 6 10）
        # 2 Good        （guesses <^ 8 10）
        # 3 Strong      （guesses < 10 ^ 10）
        # 4 Very strong （guesses >= 10 ^ 10）
     */
    /**
     * sanitizedStr 需要排除的
     */
    fun checkPwdStrength(pwd:String,sanitizedStr:String?):Int{
        if(TextUtils.isEmpty(pwd)){
            return 0;
        }
        val sanitizedList = ArrayList<String>()
        if(!TextUtils.isEmpty(sanitizedStr)){
            sanitizedList.add(sanitizedStr!!)
        }
        return Zxcvbn().measure(pwd,sanitizedList).score
    }

    /**
     * 密码包含字母和数字，字母区分大小写
     */
    fun checkPassword(password:String):Boolean{
        val pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,16})$")
        val matcher: Matcher = pattern.matcher(password)
        return matcher.matches()
    }

    /**
     * 绑定地址5开头 一共48位
     */
    fun bindAddressRule(address: String): Boolean {
        val p = Pattern.compile("^5\\w{47}$")
        val m = p.matcher(address)
        return m.matches()
    }
}